//
//  RBDateRangePickerVC.m
//  RBDemoProj
//
//  Created by Lan on 2019/1/3.
//  Copyright © 2019年 SummerTea. All rights reserved.
//

#import "RBDateRangePickerVC.h"
#import "RBDefine.h"
#import "UIViewController+RBAdd.h"
#import <UIView+YYAdd.h>

@interface RBDateRangePickerVC ()

@property(nonatomic, copy) void (^block)(NSDate *fromDate, NSDate *toDate);

@end

@implementation RBDateRangePickerVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.contentView.width = kRBMainScreenWidth;
    self.contentView.height = self.contentView.height;
    self.contentView.left = 0;
    self.contentView.top = kRBMainScreenHeight;
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
}

#pragma mark - Public

- (void)showWithMaxFromDate: (NSDate *)maxFromDate
                minFromDate: (NSDate *)minFromDate
            currentFromDate: (NSDate *)currentFromDate
                  maxToDate: (NSDate *)maxToDate
                  minToDate: (NSDate *)minToDate
              currentToDate: (NSDate *)currentToDate
                     fromVc: (UIViewController *)fromVc
             datePickerMode: (UIDatePickerMode)mode
                 completion: (void(^)(NSDate *fromDate, NSDate *toDate))block
{
    self.block = block;
    [self.view addSubview:self.contentView];
    [fromVc presentVc:self];
    self.fromDatePicker.datePickerMode = mode;
    self.fromDatePicker.maximumDate = maxFromDate;
    self.fromDatePicker.minimumDate = minFromDate;
    self.fromDatePicker.date = currentFromDate;
    self.toDatePicker.datePickerMode = mode;
    self.toDatePicker.maximumDate = maxToDate;
    self.toDatePicker.minimumDate = minToDate;
    self.toDatePicker.date = currentToDate;
    CGFloat width = (kRBMainScreenWidth - 40)/2;
    CGFloat normalWidth = 0;
    switch (mode)
    {
        case UIDatePickerModeTime:
            normalWidth = 150;
            break;
        case UIDatePickerModeDate:
            normalWidth = 250;
            break;
        case UIDatePickerModeDateAndTime:
            normalWidth = 290;
            break;
        case UIDatePickerModeCountDownTimer:
            normalWidth = 150;
            break;
        default:
            break;
    }
    CGFloat scale = 1;
    if (normalWidth <= width)
    {
        scale = 1;
    }
    else
    {
        scale = width/normalWidth;
    }
    self.fromDatePicker.transform = CGAffineTransformMakeScale(scale, scale);
    self.toDatePicker.transform = CGAffineTransformMakeScale(scale, scale);
    CGFloat y = 44;
    CGFloat w = width;
    CGFloat h = self.contentView.height - y;
    self.fromDatePicker.top = y;
    self.fromDatePicker.width = w;
    self.fromDatePicker.height = h;
    self.fromDatePicker.left = 0;
    self.toDatePicker.top = y;
    self.toDatePicker.width = w;
    self.toDatePicker.height = h;
    self.toDatePicker.right = self.contentView.width;
}

- (void)show
{
    [UIView animateWithDuration:0.05 animations:^{
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            self.contentView.bottom = kRBMainScreenHeight - kRBSafeBottomHeight;
        }];
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.15 animations:^{
        self.contentView.top = kRBMainScreenHeight;
    }];
    [UIView animateWithDuration:0.05 delay:0.1 options:0 animations:^{
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

#pragma mark - Event

- (IBAction)cancelBtnTapped:(id)sender
{
    [self dismiss];
}

- (IBAction)confirmBtnTapped:(id)sender
{
    if (self.block)
    {
        self.block(self.fromDatePicker.date, self.toDatePicker.date);
    }
    [self dismiss];
}

#pragma mark - Getter

@end
